<?php


namespace app\api\controller\v12;


use app\api\controller\v2\Auth;
use app\common\cachemodel\ZlCenterAdvertVersion;
use app\common\cachemodel\ZlUserVip;
use app\common\model\ArticleCommentReply;
use app\common\model\MessageCenter;
use app\common\model\Recode;

class Personal extends Auth
{
    /**
     * @title
     * @method GET
     * @desc  获取我的信息
     * @return array
     * @throws \app\common\lib\exception\ApiException
     * @throws \Exception
     */
    public function index()
    {
        if (!request()->isGet())
        {
            return show(config('code.error'), '未知操作', [], 403);
        }
        //判断用户是否绑定准了号
        $uen = (new \app\common\model\User())->check_uen($this->user);

        $RecodeModel = new Recode();
        $MsgModel = new MessageCenter();
        $uid = $this->user['id'];
        //        将用户头像和档案头像统一
        $map = ['uid' => $this->user['id']];
        $rea = $RecodeModel->getSelfRecode($map);
        if ($rea)
        {
            $sex = $rea['type'] == 1 ? 1 : 2;
            $recode = $RecodeModel->getRecodeWithAstro(['id' => $rea['id']]);
            $data = [
                'id' => $recode['id'],
                'uen' => $uen ?: $this->user['uen'],
                'name' => $recode['name'],
                'sex' => $sex,
                'avatar' => $recode['avatar'],
                'sun' => $recode['sun'],
                'moon' => $recode['moon'],
                'asc' => $recode['asc'],
            ];
            //获取用户全部未读系统消息id
            $system_msg_id = $MsgModel->getAllUserMessage(['uid' => $uid, 'type' => 1]);
            //获取用户全部未读服务消息id
            $server_msg_id = $MsgModel->getAllUserMessage(['uid' => $uid, 'type' => 2]);
            $msg_count = count($system_msg_id) + count($server_msg_id);

            // 获取回复的阅读情况
            $replyModel = new ArticleCommentReply();
            $unread_ids = $replyModel->getUnReadReply(['ucid' => $this->user['uc_id'], 'uid' => $this->user['id']]);
            if ($unread_ids)
            {
                $reply_count = count($unread_ids);
            } else
            {
                $reply_count = 0;
            }
            $array1['msg_count'] = $msg_count + $reply_count;
            $array = array_merge($data, $array1);

            $userVip = (new ZlUserVip())->getInfoById($uid);

            if ($userVip)
            {
                $array['is_vip_history'] = 1;
                $array['vip'] = (int)$userVip['vip_level'];
                $array['vip_is_online'] = (int)$userVip['is_valid'];
                $advertIndex = $userVip['is_valid'] ? 2 : 3; // 2续费，3激活开通

            } else
            {
                $array['is_vip_history'] = 0;
                $array['vip'] = 0;
                $array['vip_is_online'] = 0;
                $advertIndex = 1; // 开通
            }

            $array['adv_list'] = (new ZlCenterAdvertVersion())->getPersonalAdvertInfo($advertIndex);

            return show(200, '成功', $array);
        }
        return show(config('code.error'), '请添加档案');
    }
}
